<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>uplevel manual page - Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> uplevel</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
uplevel &mdash; Execute a script in a different stack frame
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>uplevel </B>?<I>level</I>?<I> arg </I>?<I>arg ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
All of the <I>arg</I> arguments are concatenated as if they had
been passed to <B><A HREF="../TclCmd/concat.htm">concat</A></B>; the result is then evaluated in the
variable context indicated by <I>level</I>.  <B>Uplevel</B> returns
the result of that evaluation.
<P>
If <I>level</I> is an integer then
it gives a distance (up the procedure calling stack) to move before
executing the command.  If <I>level</I> consists of <B>#</B> followed by
a number then the number gives an absolute level number.  If <I>level</I>
is omitted then it defaults to <B>1</B>.  <I>Level</I> cannot be
defaulted if the first <I>command</I> argument starts with a digit or <B>#</B>.
<P>
For example, suppose that procedure <B>a</B> was invoked
from top-level, and that it called <B>b</B>, and that <B>b</B> called <B>c</B>.
Suppose that <B>c</B> invokes the <B>uplevel</B> command.  If <I>level</I>
is <B>1</B> or <B>#2</B>  or omitted, then the command will be executed
in the variable context of <B>b</B>.  If <I>level</I> is <B>2</B> or <B>#1</B>
then the command will be executed in the variable context of <B>a</B>.
If <I>level</I> is <B>3</B> or <B>#0</B> then the command will be executed
at top-level (only global variables will be visible).
<P>
The <B>uplevel</B> command causes the invoking procedure to disappear
from the procedure calling stack while the command is being executed.
In the above example, suppose <B>c</B> invokes the command
<P>
<PRE><B>uplevel</B> 1 {set x 43; d}</PRE>
<P>
where <B>d</B> is another Tcl procedure.  The <B><A HREF="../TclCmd/set.htm">set</A></B> command will
modify the variable <B>x</B> in <B>b</B>'s context, and <B>d</B> will execute
at level 3, as if called from <B>b</B>.  If it in turn executes
the command
<P>
<PRE><B>uplevel</B> {set x 42}</PRE>
<P>
then the <B><A HREF="../TclCmd/set.htm">set</A></B> command will modify the same variable <B>x</B> in <B>b</B>'s
context:  the procedure <B>c</B> does not appear to be on the call stack
when <B>d</B> is executing.  The <B><A HREF="../TclCmd/info.htm">info level</A></B> command may
be used to obtain the level of the current procedure.
<P>
<B>Uplevel</B> makes it possible to implement new control
constructs as Tcl procedures (for example, <B>uplevel</B> could
be used to implement the <B><A HREF="../TclCmd/while.htm">while</A></B> construct as a Tcl procedure).
<P>
The <B><A HREF="../TclCmd/namespace.htm">namespace eval</A></B> and <B><A HREF="../TclCmd/apply.htm">apply</A></B> commands offer other ways
(besides procedure calls) that the Tcl naming context can change.
They add a call frame to the stack to represent the namespace context.
This means each <B><A HREF="../TclCmd/namespace.htm">namespace eval</A></B> command
counts as another call level for <B>uplevel</B> and <B><A HREF="../TclCmd/upvar.htm">upvar</A></B> commands.
For example, <B>info level 1</B> will return a list
describing a command that is either
the outermost procedure call or the outermost <B><A HREF="../TclCmd/namespace.htm">namespace eval</A></B> command.
Also, <B>uplevel #0</B> evaluates a script
at top-level in the outermost namespace (the global namespace).
<H3><A NAME="M5">EXAMPLE</A></H3>
As stated above, the <B>uplevel</B> command is useful for creating new
control constructs.  This example shows how (without error handling)
it can be used to create a <B>do</B> command that is the counterpart of
<B><A HREF="../TclCmd/while.htm">while</A></B> except for always performing the test after running the
loop body:
<P>
<PRE>proc do {body while condition} {
    if {$while ne &quot;while&quot;} {
        error &quot;required word missing&quot;
    }
    set conditionCmd [list expr $condition]
    while {1} {
        <B>uplevel</B> 1 $body
        if {![<B>uplevel</B> 1 $conditionCmd]} {
            break
        }
    }
}</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/apply.htm">apply</A></B>, <B><A HREF="../TclCmd/namespace.htm">namespace</A></B>, <B><A HREF="../TclCmd/upvar.htm">upvar</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#context">context</A>, <A href="../Keywords/L.htm#level">level</A>, <A href="../Keywords/N.htm#namespace">namespace</A>, <A href="../Keywords/S.htm#stack frame">stack frame</A>, <A href="../Keywords/V.htm#variable">variable</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1997 Sun Microsystems, Inc.
</div>
</BODY></HTML>
